# flask中使用的是app上下文管理，在不同的文件中使用需要将其放在可跨文件的公共变量中调用
import os

import redis
from celery import Celery

SERVER_HOST = 'http://127.0.0.1:5000/'  # 本机ip或域名，发邮件和支付用
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
NAME = 'FlaskRestful'


class Config:
    DEBUG = False
    TESTING = False
    # 数据库参数
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # Cache参数
    CACHE_TYPE = 'redis'
    CACHE_KEY_PREFIX = 'cache:'
    CACHE_REDIS_URL = 'redis://172.17.0.3:6379'  # redis和mysql都用的docker，改成自己的真实地址
    CELERY_RESULT_EXPIRES = 5 * 60  # 过期时间，单位为秒，默认值是1天

    # session参数
    SESSION_USE_SIGNER = True  # 是否加盐
    SECRET_KEY = os.urandom(24)  # 加盐后必须设置安全码
    SESSION_TYPE = 'redis'  # 以什么方式存储session
    # SESSION_COOKIE_SECURE = True  # 浏览器是否只使用基于 HTTPS 的请求发送 cookie
    SESSION_KEY_PREFIX = 'session:'  # session在存储库中的前缀
    SESSION_REDIS = redis.Redis(host='172.17.0.3', db=1)
    SESSION_PERMANENT = False  # session是否长期存储，默认为True。如果设置为False，关闭浏览器session就失效
    PERMANENT_SESSION_LIFETIME = 60 * 60  # 默认session的过期时间，单位为秒，设置的参数应为datetime.timedelta(hours/minutes/seconds)或int)

    # Celery参数
    CELERY_BROKER_URL = 'redis://172.17.0.3:6379/3'
    CELERY_RESULT_BACKEND = 'redis://172.17.0.3:6379/3'
    CELERY_TASK_RESULT_EXPIRES = 10

    MAIL_SERVER = 'smtp.office365.com'
    MAIL_USERNAME = 'flask_restful_test@outlook.com'
    MAIL_PASSWORD = 'flask_restful_mail'
    MAIL_PORT = 587  # 默认为25
    MAIL_USE_TLS = True
    MAIL_DEFAULT_SENDER = (NAME, MAIL_USERNAME)  # 可迭代二元对象:(发送者姓名，发送者邮箱)，也可只有一外发送邮箱
    # MAIL_DEFAULT_SENDER = MAIL_USERNAME


def get_db_uri(db_info):
    # 如果没有设置数据库，就默认用sqlite
    engine = db_info.get('ENGINE', 'sqlite')
    driver = db_info.get('DRIVER', '')
    user = db_info.get('USER', '')
    password = db_info.get('PASSWORD', '')
    host = db_info.get('HOST', '')
    port = db_info.get('PORT', '')
    try:
        name = db_info['NAME']
        if engine == 'sqlite':
            uri = f'sqlite:///{name}'
        else:
            uri = f'{engine}+{driver}://{user}:{password}@{host}:{port}/{name}?charset=UTF8MB4'
        return uri
    except KeyError:
        raise KeyError('你没有指定数据库，请检查setting.py中的config参数')


class DevelopmentConfig(Config):  # 根据实际情况修改配置参数
    DEBUG = True
    db_info = {
        'ENGINE': 'mysql',
        'DRIVER': 'pymysql',
        'USER': 'root',
        'PASSWORD': '',
        # 'HOST': 'localhost',
        'HOST': '172.17.0.2',
        'PORT': 3306,
        'NAME': 'restful'
    }

    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


class TestingConfig(Config):
    TESTING = True
    db_info = {
        'ENGINE': 'mysql',
        'DRIVER': 'pymysql',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': 3306,
        'NAME': 'user'
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


class StagingConfig(Config):
    db_info = {
        'ENGINE': 'mysql',
        'DRIVER': 'pymysql',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': 3306,
        'NAME': 'users'
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


class ProductionConfig(Config):
    db_info = {
        'ENGINE': 'mysql',
        'DRIVER': 'pymysql',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': 3306,
        'NAME': 'restful'
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)


envs = {
    'default': DevelopmentConfig,
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'staging': StagingConfig,
    'production': ProductionConfig
}


celery = Celery(__name__, broker=Config.CELERY_BROKER_URL)